package selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
import java.awt.Toolkit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.remote.RemoteWebDriver;

public class AddFilmWithoutRequired extends selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

@Test
  public void testFilmAddAllFields() throws Exception {    
	driver.get(baseUrl + "/php4dvd/");
	
	selenium.LoginTest.testLogin();    
    
 // Check required fields    
        
    driver.findElement(By.xpath("//a[@href='./?go=add']")).click();
       	    
    WebElement Number = 
    driver.findElement(By.name("imdbid"));
    Number.click();
    Number.sendKeys("1002");
    	    
    WebElement AlsoKnownAs = 
    driver.findElement(By.name("aka"));
    AlsoKnownAs.click();
    AlsoKnownAs.sendKeys("Железный человек 2");
    	    
    WebElement Duration =
    driver.findElement(By.name("duration"));
    Duration.click();
    Duration.sendKeys("124");
    	    
    WebElement Rating = 
    driver.findElement(By.name("rating"));
    Rating.click();
    Rating.sendKeys("10");
    	    
    WebElement Format = 
    driver.findElement(By.id("formats"));
    Format.click();
    Format.sendKeys("/Blu-ray");    
    	    
    driver.findElement(By.id("cover")).sendKeys("C:\\Users\\korotkova\\Desktop\\Auto\\iron.jpg");
    	    
    driver.findElement(By.name("trailer")).sendKeys("http://www.filmz.ru/videos/8933/");    
    
    WebElement Subtitles = 
    driver.findElement(By.name("subtitles"));
    Subtitles.click();
    Subtitles.sendKeys("yes");
        	    
    WebElement Country = 
    driver.findElement(By.name("country"));
    Country.click();
    Country.sendKeys("USA");
    	    
    WebElement Genres = 
    driver.findElement(By.name("genres"));
    Genres.click();
    Genres.sendKeys("action");
    	    
    WebElement Director =
    driver.findElement(By.name("director"));
    Director.click();
    Director.sendKeys("Jon Favreau");
    	    
    WebElement Writer = 
    driver.findElement(By.name("writer"));
    Writer.click();
    Writer.sendKeys("Justin Theroux");
    	    
    WebElement Producer = 
    driver.findElement(By.name("producer"));
    Producer.click();
    Producer.sendKeys("Stan Lee");

    WebElement Music = 
    driver.findElement(By.name("music"));
    Music.click();
    Music.sendKeys("Alex Ott");   
    	   
    driver.findElement(By.id("submit")).click();
    
 // Check Required Fields are marked
    
    WebElement TitleError = 
    driver.findElement(By.xpath("//div[@class='addmovie']//table//tr[2]/td/label"));
    Assert.assertTrue(TitleError.getText().equals("This field is required"));
    
    WebElement YearError = 
    driver.findElement(By.xpath("//div[@class='addmovie']//table//tr[4]/td/label"));
    Assert.assertTrue(YearError.getText().equals("This field is required"));
    
 // Fill only one required field   
    WebElement Title = 
    driver.findElement(By.name("name"));
    Title.click();
    Title.sendKeys("Iron Man 2");
    
    driver.findElement(By.id("submit")).click();
    
    Assert.assertFalse(TitleError.getText().equals("This field is required"));    
    
    Assert.assertTrue(YearError.getText().equals("This field is required"));
    
 // Fill all required fields
    
    WebElement Year = 
    driver.findElement(By.name("year"));
    Year.click();
    Year.sendKeys("2010");
    
    driver.findElement(By.id("submit")).click();     
   
// Check Film correctly added   
    
    WebElement FilmTitle = 
    driver.findElement(By.xpath("//div[@class='maininfo']/h2"));
    Assert.assertTrue(FilmTitle.getText().equals("Iron Man 2 (2010)"));
    
    WebElement FilmGenre = 
    driver.findElement(By.cssSelector("div.genre"));
    Assert.assertTrue(FilmGenre.getText().equals("action"));
    
    WebElement FilmDuration = 
    driver.findElement(By.cssSelector("div.duration"));
    Assert.assertTrue(FilmDuration.getText().equals("DVD/Blu-ray, 124 minutes"));
    
    Assert.assertTrue(isElementPresent(By.xpath("//div[@class='photo']/img")));    
    
    WebElement FilmRating = 
    driver.findElement(By.cssSelector("div.rating"));
    Assert.assertTrue(FilmRating.getText().contains("10"));
    
    WebElement FilmSubtitles = 
    driver.findElement(By.cssSelector("div.subtitles"));
    Assert.assertTrue(FilmSubtitles.getText().contains("yes"));
    
    WebElement FilmCountry = 
    driver.findElement(By.cssSelector("div.country"));
    Assert.assertTrue(FilmCountry.getText().contains("USA"));
    
    WebElement FilmDirector = 
    driver.findElement(By.cssSelector("div.director"));
    Assert.assertTrue(FilmDirector.getText().contains("Jon Favreau"));
    
    WebElement FilmWriter = 
    driver.findElement(By.cssSelector("div.writer"));
    Assert.assertTrue(FilmWriter.getText().contains("Justin Theroux"));
    
    WebElement FilmProducer = 
    driver.findElement(By.cssSelector("div.producer"));
    Assert.assertTrue(FilmProducer.getText().contains("Stan Lee"));
    
    WebElement FilmMusic = 
    driver.findElement(By.cssSelector("div.music"));
    Assert.assertTrue(FilmMusic.getText().contains("Alex Ott"));    
    
   }

public static void setClipboardContents(String text) {
	  StringSelection stringSelection = new StringSelection( text );
	  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	  clipboard.setContents(stringSelection, null);
  
	
}

private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
  
@AfterMethod
  public static void SiteLogout() throws Exception {
	
	selenium.LogoutTest.testLogout();
	       
  }  
  
}
